﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp1.Properties;
using static System.Windows.Forms.LinkLabel;

namespace WindowsFormsApp1
{
    /*************************************************************************************
 *
 * 文 件 名:   ModuleFlowItem
 * 描    述: 
 * 
 * 版    本：  V1.0
 * 创 建 者：  Fly
 * 创建时间：  2025/9/4 11:14:42
 *
 * --微信--：  xff19880420
 * --Email-：  xff2006@163.com
 * ======================================
 * 历史更新记录
 * 版本：V          修改时间：         修改人：
 * 修改内容：
 * ======================================
*************************************************************************************/
    /// <summary>
    /// 单元类型
    /// </summary>
    public enum UnitType
    {
        /// <summary>
        ///模块
        /// </summary>
        Mod,
        /// <summary>
        /// 模块文件夹
        /// </summary>
        Folder,
        /// <summary>
        /// 空白模块,占位用
        /// </summary>
        Empty
    }
    /// <summary>
    /// 鼠标当前区域
    /// </summary>
    public enum CheckArea
    {
        /// <summary>
        /// 空白区域
        /// </summary>
        Space,
        /// <summary>
        /// 模块图标
        /// </summary>
        Icon,
        /// <summary>
        /// 多选框
        /// </summary>
        MultiSelect,
        /// <summary>
        /// 注释
        /// </summary>
        Remarks
    }
    public class ModFlowItem : TreeNode
    {
        private Bitmap m_FolderIconOpen;
        private Bitmap m_FolderIconClose;
        /// <summary>
        /// 是否模块可选择
        /// </summary>
        internal static bool m_Checkable = false;
        public ModuleParam mModuleParam { get; }
        internal static bool dragList = false;
        /// <summary>
        /// 拖拽目标位置的UnitNo
        /// </summary>
        internal static int m_DragToUnitNo = -1;
        /// <summary>
        /// 拖拽目标位置的UnitNo
        /// </summary>
        internal static int m_DragFromUnitNo = -1;

        public ModFlowItem(ModuleParam info, int index) : base(info.ModuleName)
        {
            Top = -1;
            Left = -1;
            ModNo = index;
            mModuleParam = info;
            if (Regex.IsMatch(info.ModuleName, "文件夹[0-9]*$") || Regex.IsMatch(info.ModuleName, "坐标补正[0-9]*$") || info.ModuleName.StartsWith("如果") || info.ModuleName.StartsWith("否则") || info.ModuleName.StartsWith("循环开始"))
            {
                UnitType = UnitType.Folder;
            }
            else if (info.PluginName == "")
            {
                UnitType = UnitType.Empty;
            }
            else
            {
                UnitType = UnitType.Mod;
            }
        }
        public UnitType UnitType { get; }
        internal static int ControlWidth { get; set; } = 120;
        /// <summary>
        /// 注释的左下点
        /// </summary>
        public Point RemarksPoint { get; private set; }

        /// <summary>
        /// 检查鼠标的位置,并返回状态
        /// </summary>
        /// <param name="e">鼠标信息</param>
        /// <returns>位置枚举</returns>
        internal CheckArea HitCheck(MouseEventArgs e)
        {
            if (m_Checkable && (e.X > (ControlWidth - Height * 2)) && (e.X < (ControlWidth - Height * 2 + Height / 2.0)))
            {
                return CheckArea.MultiSelect;
            }
            if ((e.X > Left) && (e.X < (Left + Height)))
            {
                return CheckArea.Icon;
            }
            if (RemarksPoint.X < e.X && e.X < RemarksPoint.X + 90 && RemarksPoint.Y - 5 < e.Y && e.Y < RemarksPoint.Y + 9 + 5)
            {
                return CheckArea.Remarks;
            }
            return CheckArea.Space;
        }
        private void MakeProcUnitList()
        {
            int width = 32;
            int coldif = 8;
            int rowdif = 7;
            int tempWidth = (width / 3);
            m_FolderIconOpen = new Bitmap(26, 26);
            m_FolderIconClose = new Bitmap(26, 26);
            //画出图标图像  文件打开样式/文件关闭样式 26*26
            using (Graphics g = Graphics.FromImage(m_FolderIconOpen))
            {
                using (Pen pen = new Pen(Color.Blue, 2f))
                {
                    //画做左侧连接线
                    //g.DrawLine(pen, (width / 4) + tempWidth / 2 - coldif, 0, (width / 4) + tempWidth / 2 - coldif, Height);
                    //绘制折叠框白色填充
                    g.FillRectangle(new SolidBrush(Color.White), new Rectangle((width / 4) - coldif, (width / 4 + rowdif), tempWidth, tempWidth));
                    g.DrawLine(pen, (width / 4) - coldif, (width / 4) + rowdif + tempWidth / 2, (width / 4) + tempWidth - coldif, (width / 4) + rowdif + tempWidth / 2);
                    //绘制折叠框边框
                    g.DrawRectangle(pen, new Rectangle((width / 4) - coldif, (width / 4) + rowdif, tempWidth, tempWidth));
                }
            }
            using (Graphics g = Graphics.FromImage(m_FolderIconClose))
            {
                //using (Pen pen = new Pen(Color.Blue, 1f))
                //{
                //    //绘制折叠框白色填充
                //    g.FillRectangle(new SolidBrush(Color.White), new Rectangle((width / 4) - coldif, (width / 4 + rowdif), tempWidth, tempWidth));
                //    g.DrawLine(pen, (width / 4) - coldif, (width / 4) + rowdif + tempWidth / 2, (width / 4) + tempWidth - coldif, (width / 4) + rowdif + tempWidth / 2);
                //    g.DrawLine(pen, (width / 4) + tempWidth / 2 - coldif, 15, (width / 4) + tempWidth / 2 - coldif, Height);
                //    //绘制折叠框边框
                //    g.DrawRectangle(pen, new Rectangle((width / 4) - coldif, (width / 4) + rowdif, tempWidth, tempWidth));
                //}

                using (Pen pen = new Pen(Color.Blue, 1f))
                {
                    //画做左侧连接线
                    //g.DrawLine(pen, (width / 4) + tempWidth / 2 - coldif, 0, (width / 4) + tempWidth / 2 - coldif, Height);
                    //绘制折叠框白色填充
                    //g.FillRectangle(new SolidBrush(Color.White), new Rectangle((width / 4) - coldif, (width / 4 + rowdif), tempWidth, tempWidth));
                    g.DrawLine(pen, (width / 4) - coldif, (width / 4) + rowdif + tempWidth / 2, (width / 4) + tempWidth - coldif+1000, (width / 4) + rowdif + tempWidth / 2);
                    //绘制折叠框边框
                    //g.DrawRectangle(pen, new Rectangle((width / 4) - coldif, (width / 4) + rowdif, tempWidth, tempWidth));
                }
            }
        }

        private void MakeProcUnitList1()
        {
            int width = 32;
            int coldif = 8;
            int rowdif = 7;
            int tempWidth = (width / 3);
            m_FolderIconOpen = new Bitmap(26, 26);
            m_FolderIconClose = new Bitmap(26, 26);
            //画出图标图像  文件打开样式/文件关闭样式 26*26
            using (Graphics g = Graphics.FromImage(m_FolderIconOpen))
            {
                using (Pen pen = new Pen(Color.Blue, 2f))
                {
                    //画做左侧连接线
                    //g.DrawLine(pen, (width / 4) + tempWidth / 2 - coldif, 0, (width / 4) + tempWidth / 2 - coldif, Height);
                    //绘制折叠框白色填充
                    g.FillRectangle(new SolidBrush(Color.White), new Rectangle((width / 4) - coldif, (width / 4 + rowdif), tempWidth, tempWidth));
                    g.DrawLine(pen, (width / 4) - coldif, (width / 4) + rowdif + tempWidth / 2, (width / 4) + tempWidth - coldif, (width / 4) + rowdif + tempWidth / 2);
                    //绘制折叠框边框
                    g.DrawRectangle(pen, new Rectangle((width / 4) - coldif, (width / 4) + rowdif, tempWidth, tempWidth));
                }
            }
            using (Graphics g = Graphics.FromImage(m_FolderIconClose))
            {
                //using (Pen pen = new Pen(Color.Blue, 1f))
                //{
                //    //绘制折叠框白色填充
                //    g.FillRectangle(new SolidBrush(Color.White), new Rectangle((width / 4) - coldif, (width / 4 + rowdif), tempWidth, tempWidth));
                //    g.DrawLine(pen, (width / 4) - coldif, (width / 4) + rowdif + tempWidth / 2, (width / 4) + tempWidth - coldif, (width / 4) + rowdif + tempWidth / 2);
                //    g.DrawLine(pen, (width / 4) + tempWidth / 2 - coldif, 15, (width / 4) + tempWidth / 2 - coldif, Height);
                //    //绘制折叠框边框
                //    g.DrawRectangle(pen, new Rectangle((width / 4) - coldif, (width / 4) + rowdif, tempWidth, tempWidth));
                //}

                using (Pen pen = new Pen(Color.Blue, 1f))
                {
                    //画做左侧连接线
                    //g.DrawLine(pen, (width / 4) + tempWidth / 2 - coldif, 0, (width / 4) + tempWidth / 2 - coldif, Height);
                    //绘制折叠框白色填充
                    //g.FillRectangle(new SolidBrush(Color.White), new Rectangle((width / 4) - coldif, (width / 4 + rowdif), tempWidth, tempWidth));
                    g.DrawLine(pen, (width / 4) - coldif, (width / 4) + rowdif + tempWidth / 2, (width / 4) + tempWidth - coldif + 1000, (width / 4) + rowdif + tempWidth / 2);
                    //绘制折叠框边框
                    //g.DrawRectangle(pen, new Rectangle((width / 4) - coldif, (width / 4) + rowdif, tempWidth, tempWidth));
                }
            }
        }

        /// <summary>
        /// 画出一个单元对象
        /// </summary>
        /// <param name="g"></param>
        /// <param name="left"></param>
        /// <param name="top"></param>
        /// <param name="w"></param>
        /// <param name="h"></param>
        /// <param name="nest"></param>
        /// <param name="parent_top"></param>
        internal void PaintItem(Graphics g, ref int left, ref int top, int w, int h, int nest, string mSpaceKey, ref int parent_top, bool selected)
        {
            int num = (h * 3 / 4);//22
            int num2 = (int)(h * 0.1);//3
            Top = top;
            Left = (nest * num) + left;//
            Width = w;
            Height = h;
            int num8 = true ? 10 : 10;//偏移量x 改为一样 就没有动画效果 
            Font font = /*TreeView != null ? TreeView.Font :*/ new Font("宋体", 10F, FontStyle.Regular);
            using (SolidBrush brush = new SolidBrush(Color.White))
            {
                using (Pen pen2 = new Pen(Color.Blue))
                {
                    g.DrawLine(pen2, left + (nest * num) - 12 + 20, top, ControlWidth, top);
                    //-------------------选中的背景效果 整体填充
                    if (IsSelected /*&& !Solution.Instance.GetStates()*/)
                    {
                        brush.Color = Color.PeachPuff;//运行的时候 不再显示选择效果
                        g.FillRectangle(brush, Left + 40, top + 1, Width, h - 1);
                    }
                    brush.Color = ColorTranslator.FromHtml("#000000");
                     //画模块名称 编码   以及耗时
                    g.DrawString(ModNo.ToString() + "." + mModuleParam.ModuleName, font, brush, Left + Height + 10, (float)(top + (h / 4)) - 3);
                    g.DrawString(mModuleParam.CostTime.ToString("F1").PadLeft(4, ' ') + "ms", font, brush, ControlWidth - 60, (float)(top + (h / 4)) - 3);

                    //画出描述文字 subtext 
                    using (Font font2 = new Font(font.Name, 9F, FontStyle.Regular))
                    {
                        brush.Color = Color.DarkGreen;
                        RemarksPoint = new Point(((Left + Height) + 4) + 20, (top + (h / 2) + (h / 8)));
                        g.DrawString(mModuleParam.ModuleRemarks + "->(" + mModuleParam.WindowName + ")", font2, brush, RemarksPoint.X, RemarksPoint.Y);
                    }
                    int num4 = Top + (Height / 2); //最左侧树列表的竖线
                    pen2.Color = Color.Blue;
                    //最左侧树列表的竖线 7为固定左边距离
                    g.DrawLine(pen2, 7, top, 7, top + Height);
                    //-------------------拖拽插入的效果
                    //Debug.WriteLine("dragList:" + dragList);
                   // Debug.WriteLine("ModNo" + ModNo+ "  --------;m_DragToUnitNo"+ m_DragToUnitNo+ "   -m_DragFromUnitNo"+ m_DragFromUnitNo);
                   
                    if ((dragList) && (ModNo == m_DragToUnitNo) && m_DragToUnitNo != m_DragFromUnitNo)
                    {
                        brush.Color = Color.Red;
                        g.FillRectangle(brush, 0, top + Height - 1, ControlWidth, 2);
                        brush.Color = Color.Black;
                    }

                    brush.Color = Color.Black;
                    {
                        int x = ControlWidth - Height - Height / 8;
                        int y = top + (Height / 4);
                        g.DrawImage(Resources.正确, new Rectangle(x + 15, y + 7, 16, 16), new Rectangle(0, 0, 32, 32), GraphicsUnit.Pixel);

                    }
                    switch (UnitType)
                    {
                        case UnitType.Mod:
                            {
                                Icon image2 = mModuleParam.Icon;
                                g.DrawIcon(image2, new Rectangle(Left + num2 + num8, (top + num2) + 0, 26, 26));
                                break;
                            }
                           
                        case UnitType.Folder:
                            {
                                int width = Height - (num2 * 2);
                                int num7 = 2; /*Click ? 2 : 0;*///偏移量y
                                int x = Left + num2 + num8;
                                int y = Top + num2 + num7;
                                MakeProcUnitList();

                                //using (Pen pen = new Pen(Color.Blue, 2f))
                                //{
                                //    //绘制折叠框白色填充
                                //    g.FillRectangle(new SolidBrush(Color.White), new Rectangle((width / 4) - coldif, (width / 4 + rowdif), tempWidth, tempWidth));
                                //    g.DrawLine(pen, (width / 4) - coldif, (width / 4) + rowdif + tempWidth / 2, (width / 4) + tempWidth - coldif, (width / 4) + rowdif + tempWidth / 2);
                                //    //绘制折叠框边框
                                //    g.DrawRectangle(pen, new Rectangle((width / 4) - coldif, (width / 4) + rowdif, tempWidth, tempWidth));
                                //}
                                using (Pen pen1 = new Pen(Color.Blue, 1f))
                                {
                                    int width1 = 32;
                                    int coldif = 8;
                                    int rowdif = 7;
                                    int tempWidth = (width1 / 3);
                                    //绘制折叠框白色填充
                                    g.FillRectangle(new SolidBrush(Color.White), new Rectangle(x - num8, Top+Height/2, tempWidth, tempWidth));
                                     g.DrawLine(pen1, x - num8, Top + Height / 2+ tempWidth / 2, x - num8+ tempWidth, Top + Height / 2+ tempWidth / 2);
                                    g.DrawLine(pen1, left+7, Top + Height / 2, left+7, Top + Height / 2+ tempWidth);
                                    //绘制折叠框边框
                                    g.DrawRectangle(pen1, new Rectangle(x - num8, Top + Height/2, tempWidth, tempWidth));
                                }

                                //{
                                //    //if (m_ImageIconDic.ContainsKey(mModuleParam.PluginName))//-------------------画文件夹关闭按钮
                                //    {
                                //        //-------------------依据判定结果获取图标  //图标绘制只有一个 已经添加了结果显示图标 √ × 
                                //        Icon image2 = mModuleParam.Icon;
                                //        g.DrawIcon(image2, new Rectangle(x, y, width, width));
                                //    }
                                //    g.DrawImage(m_FolderIconClose, new Rectangle(x - num8, y, width, width), new Rectangle(0, 0, 26, 26), GraphicsUnit.Pixel);
                                //    //折叠后仍然需要在底部画出一条线 因为下一个没有画出这条断线
                                //    //g.DrawLine(pen2, left + (nest * num) - 15 + 20, top + Height, ControlWidth, top + Height);
                                //    if (m_Checkable) { DrawCheckBox(g, top, pen2, brush); }
                                //    //top += h;
                                //}
                                break;
                            }
                    }
   
                    if (m_Checkable)//-------------------画多选框
                    {
                        DrawCheckBox(g, top, pen2, brush);
                    }
                    top += h;
                }
            }
        }
        /// <summary>
        /// 画选框
        /// </summary>
        /// <param name="g"></param>
        /// <param name="top"></param>
        /// <param name="pen"></param>
        /// <param name="brush"></param>
        private void DrawCheckBox(Graphics g, int top, Pen pen, SolidBrush brush)
        {
            //if (mModuleParam.ModuleName != "")
            {
                brush.Color = Color.Black;
                int x = ControlWidth - Height * 2;
                int y = top + (Height / 4);
                g.FillRectangle(brush, x, y, Height / 2, Height / 2);
                brush.Color = Color.White;
                g.FillRectangle(brush, (x + 2), (y + 2), ((Height / 2) - 4), ((Height / 2) - 4));
                //if (m_Checked)
                {
                    pen.Color = Color.Blue;
                    pen.Width = 2.0f;
                    g.DrawLine(pen, (x + (Height / 8)), ((y + (Height / 4)) + (Height / 8)), (x + (Height / 8)), (y + (Height / 4)));
                    g.DrawLine(pen, (x + (Height / 8)), ((y + (Height / 4)) + (Height / 8)), ((x + (Height / 4)) + (Height / 8)), (y + (Height / 8)));
                }
                pen.Color = Color.Black;
                pen.Width = 1.0f;
            }
        }
        public new bool IsSelected { get; internal set; }

        public int ModNo { get; set; }
        public int Width { get; private set; }
        public int Height { get; private set; }
        public int Left { get; private set; }
        public int Top { get; private set; }

    }
}
